<?php


namespace App\Http\Controllers\Marketing\Web;


use App\Enums\BaseErrorCode;
use App\Http\Controllers\BaseController;
use App\Models\Marketing\ShopAdvertisementModel;
use App\Models\Marketing\ShopAdvertisementResourcesModel;
use App\Service\Marketing\ShopAdvertisementService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class ShopAdvertisementController extends BaseController
{
    public function list(Request $request): JsonResponse
    {
        $pageSize       = (int)$request->get('pageSize', 10);
        $pageNum        = (int)$request->get('pageNum', 1);
        $channel        = $request->get('channel');
        $title          = $request->get('title', '');
        $type           = $request->get('type');
        $status         = $request->get('status');
        $shopIds        = $request->get('shopIds', '');
        $createdAtStart = $request->get('createdAtStart', '');
        $createdAtEnd   = $request->get('createdAtEnd', '');
        $createdByUids  = $request->get('createdByUids', '');

        return $this->success(
            ShopAdvertisementService::list(
                $pageSize, $pageNum, $channel, $title, $type, $status, $shopIds, $createdAtStart, $createdAtEnd,
                $createdByUids
            )
        );
    }

    public function upload(Request $request): JsonResponse
    {
        $input     = $request->all();
        $validator = Validator::make($input, [
            'file'     => 'required|mimetypes:' . join(
                    ',', ShopAdvertisementResourcesModel::supportMineType()
                ),
            'platform' => 'required|int|' . Rule::in(
                    array_keys(ShopAdvertisementResourcesModel::RESOURCE_PLATFORM_MAP)
                ),
            'type'     => 'required|int|' . Rule::in(
                    array_keys(ShopAdvertisementResourcesModel::RESOURCE_TYPE_MAP)
                ),
        ], [
            'file.required'     => '资源必须上传',
            'file.mimetypes'    => '不支持的格式',
            'platform.required' => '平台必选',
            'platform.int'      => '平台必须是数字',
            'platform.in'       => '平台类型异常',
            'type.required'     => '类型必选',
            'type.in'           => '类型异常',

        ]);
        if ($validator->fails()) {
            return $this->fail(BaseErrorCode::$BASE_CODE_UPDATE_FAIL, join(';', $validator->errors()->all()));
        }

        return $this->success(
            ShopAdvertisementService::upload($request->file('file'), $input['platform'], $input['type'])
        );
    }

    public function info(Request $request): JsonResponse
    {
        $id = $request->get('id');
        return $this->success(ShopAdvertisementService::editInfo($id));
    }

    public function save(Request $request): JsonResponse
    {
        $input     = $request->all();
        $validator = Validator::make($input, [
            'id'                   => 'int',
            'channel'              => 'required|int|' . Rule::in(array_keys(ShopAdvertisementModel::CHANNEL_MAP)),
            'title'                => 'required|string|max:60',
            'resources.*.platform' => 'required|int|' . Rule::in(
                    array_keys(ShopAdvertisementResourcesModel::RESOURCE_PLATFORM_MAP)
                ),
            'resources.*.type'     => 'required|int|' . Rule::in(
                    array_keys(ShopAdvertisementResourcesModel::RESOURCE_TYPE_MAP)
                ),
            'resources.*.url'      => 'required|string',
            'jumpStatus'           => 'required|int|' . Rule::in(array_keys(ShopAdvertisementModel::JUMP_STATUS_MAP)),
            'cutDown'              => 'int',
            'startAt'              => 'required|string|date',
            'endAt'                => 'required|string|date',
            'showRule'             => 'required|int|' . Rule::in(array_keys(ShopAdvertisementModel::SHOW_RULE_MAP)),
            'shopScope'            => 'required|int|' . Rule::in(array_keys(ShopAdvertisementModel::SHOP_SCOPE_MAP)),
            'shopIds'              => 'array',
            'notLoginShow'         => 'int|' . Rule::in(array_keys(ShopAdvertisementModel::NOT_LOGIN_SHOW_MAP)),
            'jumpType'             => 'required|int|' . Rule::in(array_keys(ShopAdvertisementModel::JUMP_TYPE_MAP)),
            'jumpRelId'            => 'int',
            'jumpBtnShow'          => 'required|int|' . Rule::in(array_keys(ShopAdvertisementModel::JUMP_BTN_SHOW_MAP)),
            'jumpLink'             => 'string',

        ], [
            'channel.required'              => '渠道必选',
            'title.required'                => '标题必填写',
            'title.max'                     => '标题限制30个中文字',
            'resources.*.platform.required' => '资源平台必选',
            'resources.*.type.required'     => '资源类型必选',
            'resources.*.url.required'      => '资源必须上传',

            'jumpStatus.required' => '是否可跳转必填',
            'cutDown.int'         => '倒计时类型异常',
            'startAt.required'    => '有效期开始时间必选',
            'endAt.required'      => '有效期结束时间必选',
            'showRule.required'   => '显示规则必选',
            'shopScope.required'  => '门店范围必填',
            'jumpType.required'   => '跳转类型必选',
        ]);
        if ($validator->fails()) {
            return $this->fail(BaseErrorCode::$BASE_CODE_UPDATE_FAIL, join(';', $validator->errors()->all()));
        }
        ShopAdvertisementService::save($input, request()->user()->id);
        return $this->success();
    }

    public function status(Request $request, int $id): JsonResponse
    {
        $input = $request->merge(['id' => $id])->all();

        $validator = Validator::make($input, [
            'id'     => 'required|int',
            'status' => 'required|int|' . Rule::in(array_keys(ShopAdvertisementModel::STATUS_MAP))
        ], [
            'id.required'     => 'id缺失',
            'status.required' => 'status缺失',
            'status.in'       => 'status不合法',
        ]);
        if ($validator->fails()) {
            return $this->fail(BaseErrorCode::$BASE_CODE_UPDATE_FAIL, join(';', $validator->errors()->all()));
        }

        ShopAdvertisementService::changeStatus($id, $input['status']);

        return $this->success();
    }

}